/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.rest;

import cz.muni.fi.pa165.mushroom.website.dto.MushroomDto;
import cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO;
import cz.muni.fi.pa165.mushroom.website.servicesImpl.MushroomServiceImpl;
import java.net.URI;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller for REST mushroom API
 * 
 * @author Roman Jakubco <roman.jakubco@gmail.com>
 */
@Path("/mushroom")
public class MushroomRestController {
    @Autowired    
    private MushroomServiceImpl mushroomServiceImpl;    
        
    @Context
    private UriInfo context;  
    
 
    @GET
    @Path("listMushrooms")
    @Produces(MediaType.APPLICATION_JSON)
    public List<MushroomDto> listForests() {            
        List<MushroomDto> mds = mushroomServiceImpl.listMushrooms();            
        return mds;
    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("createMushroom")
    public Response createMushroom(MushroomDto mushroomDto) {
        mushroomServiceImpl.createMushroom(mushroomDto);
        System.out.println("Created mushroom " + mushroomDto.getId());
        return Response.created(URI.create(context.getAbsolutePath() + "/"+ mushroomDto.getId())).build();
    }
    
    @GET
    @Path("getMushroom/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public MushroomDto getMushroom(@PathParam("id") Long id) {
        return mushroomServiceImpl.findMushroom(id);
    }

    @GET
    @Path("getMushroomsType/{type}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<MushroomDto> listMushroomsWithType(@PathParam("type") MushroomTypeDTO dTO) {
        return mushroomServiceImpl.listMushroomsWithType(dTO);
    }   
   
    
    @PUT
    @Path("editMushroom")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateMushroom(MushroomDto mushroomDto) {
        mushroomServiceImpl.updateMushroom(mushroomDto);
        System.out.println("Updated mushroom: " + mushroomDto);
        return Response.created(URI.create(context.getAbsolutePath() + "/"+ mushroomDto.getId())).build();
    }

    @DELETE
    @Path("deleteMushroom/{id}")
    public Response deleteMushroom(@PathParam("id") Long id) {
        MushroomDto md = mushroomServiceImpl.findMushroom(id);
        mushroomServiceImpl.deleteMushroom(md);
        System.out.println("Deleted mushroom: " + md);
        return Response.created(URI.create(context.getAbsolutePath() + "/"+ md)).build();
    }
}
